package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ExamRoomEntity;
import com.tfjybj.itoo.exam.entity.ExaminationEntity;
import com.tfjybj.itoo.exam.model.CourseScheduleModel;
import com.tfjybj.itoo.exam.model.ExaminationModel;
import com.tfjybj.itoo.exam.model.ExamineeModel;
import com.tfjybj.itoo.exam.provider.service.ExamRoomService;
import com.tfjybj.itoo.exam.provider.service.ExaminationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * ExaminationController
 * examination表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"examination表接口"})
@RequestMapping(value = "/examination")
@RestController
@Slf4j
public class ExaminationController {

    @Resource
    private ExaminationService examinationService;

    @Resource
    private ExamRoomService examRoomService;
    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ExaminationModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody ExaminationModel model) {
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getExamName())) {
            return ItooResult.build(ItooResult.FAIL, "examName为空");
        }
        if (StringUtils.isEmpty(model.getClassifyId())) {
            return ItooResult.build(ItooResult.FAIL, "examClassifyId为空");
        }
        if (StringUtils.isEmpty(model.getTemplateId())) {
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
        if (StringUtils.isEmpty(model.getStartTime())) {
            return ItooResult.build(ItooResult.FAIL, "startTime为空");
        }
        if (StringUtils.isEmpty(model.getEndTime())) {
            return ItooResult.build(ItooResult.FAIL, "endTime为空");
        }
        if (StringUtils.isEmpty(model.getTemplateType())) {
            return ItooResult.build(ItooResult.FAIL, "templateType为空");
        }
        if (StringUtils.isEmpty(model.getState())) {
            return ItooResult.build(ItooResult.FAIL, "state为空");
        }
        if (StringUtils.isEmpty(model.getExamDuration())) {
            return ItooResult.build(ItooResult.FAIL, "examDuration为空");
        }
        ExaminationEntity examinationEntity = new ExaminationEntity();
        BeanUtils.copyProperties(model, examinationEntity);
        examinationService.save(examinationEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 根据考试id,删除考试,考场,考生
     *
     * @param examId 考试id
     * @return 受影响行数
     * @author maying
     * @since 2019-1-20 16:25:53
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{examId}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String examId) {
        examinationService.singleDelete(examId);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        examinationService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ExaminationModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改examination")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ExaminationModel model) {
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getExamName())) {
            return ItooResult.build(ItooResult.FAIL, "examName为空");
        }
        if (StringUtils.isEmpty(model.getClassifyId())) {
            return ItooResult.build(ItooResult.FAIL, "examClassifyId为空");
        }
        if (StringUtils.isEmpty(model.getTemplateId())) {
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
        if (StringUtils.isEmpty(model.getStartTime())) {
            return ItooResult.build(ItooResult.FAIL, "startTime为空");
        }
        if (StringUtils.isEmpty(model.getEndTime())) {
            return ItooResult.build(ItooResult.FAIL, "endTime为空");
        }
        if (StringUtils.isEmpty(model.getTemplateType())) {
            return ItooResult.build(ItooResult.FAIL, "templateType为空");
        }
        if (StringUtils.isEmpty(model.getState())) {
            return ItooResult.build(ItooResult.FAIL, "state为空");
        }
        if (StringUtils.isEmpty(model.getExamDuration())) {
            return ItooResult.build(ItooResult.FAIL, "examDuration为空");
        }
        ExaminationEntity examinationEntity = new ExaminationEntity();
        BeanUtils.copyProperties(model, examinationEntity);
        examinationService.updateById(examinationEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Examination
     *
     * @param id 主键id
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        ExaminationEntity examinationEntity = examinationService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinationEntity);
    }

    /**
     * 分页查询所有Examination
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有Examination")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ExaminationEntity> examinations = examinationService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinations);
    }

    /**
     * 根据课程id模糊查询examination
     *
     * @param courseId 课程id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的examination
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据课程id模糊查询examination", notes = "分页根据课程id模糊查询examination")
    @GetMapping(value = "queryExaminationByLikeCourseId/{pageNo}/{pageSize}")
    public ItooResult queryExaminationByLikeCourseId(@RequestParam(required = false, defaultValue = "") String courseId,
                                                     @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                     @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ExaminationEntity> examinationList = examinationService.queryByLikeCourseId(courseId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinationList);
    }
    //endregion
    /* **********************************以下为非模板生成的内容********************************* */


    /**
     * 查询所有考试课程
     *
     * @return courseName
     * @author 王雪芬
     * @since 2.0.0     2018/11/16 9:52
     */
    @ApiOperation(value = "查询所有考试科目")
    @GetMapping(value = {"/queryCourse"})
    public ItooResult queryCourse() {
        List<CourseScheduleModel> courseName = examinationService.queryCourseName();
        if (!CollectionUtils.isEmpty(courseName)) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseName);
        }
        return ItooResult.build(ItooResult.FAIL, "没有考试科目");
    }

    /**
     * 根据课程id查询考试名称
     *
     * @param examcourseid String
     * @author 王雪芬
     * @since 2.0.0   2018-11-19 12:04:29
     */
    @ApiOperation(value = "根据课程id查询考试名称")
    @GetMapping(value = {"/queryExamName/{examcourseid}"})
    public ItooResult queryExamName(@ApiParam(value = "课程id", required = true) @PathVariable String examcourseid) {
        if (StringUtils.isEmpty(examcourseid)) {
            return ItooResult.build(ItooResult.FAIL, "课程id不能为空");
        }
        List<ExaminationModel> examinationModels = examinationService.queryExamName(examcourseid);
        if (CollectionUtils.isEmpty(examinationModels)) {
            return ItooResult.build(ItooResult.FAIL, "考试名称为空");
        }
        return ItooResult.build(ItooResult.SUCCESS, "查询考试名称成功", examinationModels);

    }

    /**
     * @param examId 考试id
     * @return 成功  1111 ，失败  0000
     * @author maying
     * @since 2018年11月16日21:02:50
     * 导出功能
     */
    @ApiOperation(value = "导出Excel表格", notes = "根据用户id导出Excel")
    @GetMapping(value = "/exportExcel/{examId}")
    public void exportExcel(HttpServletResponse response, @PathVariable String[] examId) {
        examinationService.expotrExcel(response, examId);
    }

    /**
     * 根据教师账号查询老师分页所有考试
     *
     * @param operator 教师ID
     * @return 教师所有考试
     * @author 谢妞
     * @since 2018年12月14日19:09:12
     */
    @ApiOperation(value = "根据教师账号查询老师所有考试，并且按照创建考试时间排列")
    @GetMapping(value = "/queryExamByTeacherId/{operator}")
    public ItooResult queryExamByTeacherId(@PathVariable String operator) {
        if (StringUtils.isEmpty(operator)) {
            return ItooResult.build(ItooResult.FAIL, "教师编号不能为空");
        }
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinationService.queryExamByTeacherId(operator));

    }

    /**
     * 根据教师账号查询老师分页所有考试(用在成绩分析页面的树的加载中)
     *
     * @param operator 教师ID
     * @return 教师所有考试
     * @author 孙博雅
     * @since 2019年3月10日20:07:08
     */
    @ApiOperation(value = "根据教师账号查询老师所有考试，并且按照创建考试时间排列")
    @GetMapping(value = "/queryExamByTeacherIds/{operator}")
    public ItooResult queryExamByTeacherIds(@PathVariable String operator) {
        if (StringUtils.isEmpty(operator)) {
            return ItooResult.build(ItooResult.FAIL, "教师编号不能为空");
        }
        List<ExaminationModel> examinationModel = examinationService.queryExamByTeacherId(operator);
        examinationModel = examinationModel.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                // 利用 TreeSet 的排序去重构造函数来达到去重元素的目的
                // 根据firstName去重
                () -> new TreeSet<>(Comparator.comparing(ExaminationModel::getParentId))), ArrayList::new));

        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinationModel);

    }

    /**
     * 下拉框加载
     *
     * @param operator 教师ID
     * @return 教师所有信息
     * @author 谢妞
     * @since 2018年12月14日19:09:12
     */
    @ApiOperation(value = "根据教师账号查询下拉框信息")
    @GetMapping(value = "/queryDropDown/{operator}/{type}")
    public ItooResult queryDropDown(@PathVariable String operator,
                                    @PathVariable Integer type
    ) {
        if (StringUtils.isEmpty(operator)) {
            return ItooResult.build(ItooResult.FAIL, "教师编号不能为空");
        }
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinationService.queryDropDown(operator, type));

    }


    /**
     * 组合查询，根据考试id，分类id，考试开始时间，结束时间，班级ID
     *
     * @param examId     考试Id
     * @param classifyId 分类id
     * @param classId    班级id
     * @return 符合条件的教师所有考试
     * @author 谢妞
     * @since 2018年12月14日19:09:12
     */
    @ApiOperation(value = "组合查询，根据考试id，分类id，考试开始时间，结束时间，班级ID")
    @GetMapping(value = "/queryCombine")
    public ItooResult queryCombine(@RequestParam(value = "operator") String operator,
                                   @RequestParam(value = "examId", required = false) String examId,
                                   @RequestParam(value = "classifyId", required = false) String classifyId,
                                   @RequestParam(value = "classId", required = false) String classId) {

        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinationService.queryCombine(operator, examId, classifyId, classId));

    }

    /**
     * 组合查询，根据考试id，分类id，考试开始时间，结束时间，班级ID(用在成绩分析页面树的加载中)
     *
     * @param examId     考试Id
     * @param classifyId 分类id
     * @param classId    班级id
     * @return 符合条件的教师所有考试
     * @author 孙博雅
     * @since 2019年3月10日20:07:22
     */
    @ApiOperation(value = "组合查询，根据考试id，分类id，考试开始时间，结束时间，班级ID")
    @GetMapping(value = "/queryCombines")
    public ItooResult queryCombines(@RequestParam(value = "operator") String operator,
                                    @RequestParam(value = "examId", required = false) String examId,
                                    @RequestParam(value = "classifyId", required = false) String classifyId,
                                    @RequestParam(value = "classId", required = false) String classId) {

        List<ExaminationModel> examinationModel = examinationService.queryCombine(operator, examId, classifyId, classId);
        examinationModel = examinationModel.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                // 利用 TreeSet 的排序去重构造函数来达到去重元素的目的
                // 根据firstName去重
                () -> new TreeSet<>(Comparator.comparing(ExaminationModel::getExamId))), ArrayList::new));

        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examinationModel);

    }

    /**
     * @param tableArray 数组
     * @return 删除条数
     * @author maying
     * @since 2018年11月17日21:28:38
     * 批量删除
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping(value = "/batchDelete")
    public ItooResult batchDelete(@RequestBody List<String> tableArray) {
        if (tableArray.size() > 0) {
            return examinationService.arrayDelete(tableArray);
        }
        return ItooResult.build(ItooResult.FAIL, "删除失败！");
    }

    /**
     * @param examId 考试id
     * @return 删除条数
     * @author maying
     * @since 2018年11月17日21:28:38
     * 删除
     */
    @ApiOperation(value = "单条删除")
    @DeleteMapping(value = "/singleDelete/{examId}")
    public ItooResult singleDelete(@PathVariable String examId) {
        if (StringUtils.isEmpty(examId)) {
            return ItooResult.build(ItooResult.FAIL, "删除失败！");
        }
        return examinationService.singleDelete(examId);
    }

    /**
     * 根据考试状态查询所有考试信息table表格
     *
     * @param state      状态
     * @param stateTwo   状态
     * @param operatorId 登录人id
     * @return datatable表
     * @author maying
     * @since 2018年11月18日16:00:38
     */
    @ApiOperation(value = "根据考试状态查询所有考试信息table表格")
    @GetMapping(value = "/selAllExamation/{state}/{stateTwo}/{operatorId}")
    public ItooResult selAllExamation(
            @PathVariable Integer state,
            @PathVariable Integer stateTwo,
            @PathVariable String operatorId) {
        List<ExaminationModel> exam = examinationService.selAllExamation(state, stateTwo, operatorId.trim());
        return ItooResult.build(ItooResult.SUCCESS, "成功", exam);
    }

    /**
     * 首页正在进行开始信息显示
     *
     * @param state      状态
     * @param stateTwo   状态
     * @param operatorId 登录人id
     * @return datatable表
     * @author 任晓帅
     * @since 2019年5月9日16:04:46
     */
    @ApiOperation(value = "首页正在进行开始信息显示")
    @GetMapping(value = "/selExaming/{state}/{stateTwo}/{operatorId}")
    public ItooResult selExaming(
            @PathVariable Integer state,
            @PathVariable Integer stateTwo,
            @PathVariable String operatorId) {
        List<ExaminationModel> exam = examinationService.selExaming(state, stateTwo, operatorId.trim());
        return ItooResult.build(ItooResult.SUCCESS, "成功", exam);
    }

    /**
     * @param examId 考试id
     * @return 考场List
     * @author maying
     * @since 2018年11月18日16:00:38
     * 根据考试id查询考场
     */
    @ApiOperation(value = "根据考试id查询考场")
    @GetMapping(value = "/selExamRoom/{examId}")
    public ItooResult selExamRoom(@PathVariable String examId) {
        if (StringUtils.isEmpty(examId)) {
            return ItooResult.build(ItooResult.FAIL, "考场id不能为空！");
        }

        List<ExamRoomEntity> examRoomList = examRoomService.selexamRoom(examId);
        if (examRoomList.size() >= 0) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", examRoomList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询失败");
        }


    }

    /**
     * 添加学生
     *
     * @param examineeModel ExamineeModel
     * @return 添加的结果
     * @author maying
     * @since 2018-11-22 15:51:29
     */
    @ApiOperation(value = "添加考生")
    @PostMapping(value = {"/addExaminee"})
    public ItooResult addExaminee(@RequestBody ExamineeModel examineeModel) {
        if (ObjectUtils.isEmpty(examineeModel)) {
            return ItooResult.build(ItooResult.FAIL, "添加失败！");
        }
        try {
            boolean fses = examinationService.insertExaminee(examineeModel);
            return ItooResult.build(ItooResult.SUCCESS, "添加成功！", fses);
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "添加失败！");
        }
    }

    /**
     * @param examId 考试id
     * @return 考试时长
     * @author maying
     * @since 2018-11-22 20:50:51
     */

    @ApiOperation(value = "根据考试id查询考试时长")
    @GetMapping(value = "/findExamDuration/{examId}")
    public ItooResult findExamDuration(@PathVariable String examId) {
        if (StringUtils.isEmpty(examId)) {
            return ItooResult.build(ItooResult.FAIL, "id不能为空");
        }
        try {
            long examDuration = examinationService.selfindExamDuration(examId);
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", examDuration);
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "错误原因" + e);
        }
    }

    /**
     * 编辑提交功能
     *
     * @return 编辑影响行数
     * @author maying
     * @since 2018-11-23 14:01:30
     */
    @ApiOperation(value = "编辑考试")
    @PostMapping(value = "/editExam")
    public ItooResult editExam(@RequestBody ExaminationModel examineeModel) {

        List<ExaminationEntity> editsum = examinationService.editExam(examineeModel.getExamName());
        for (ExaminationEntity examinationEntity : editsum) {
            if (examinationEntity.getExamName().equals(examineeModel.getExamName()) && !examinationEntity.getId().equals(examineeModel.getId())) {
                return ItooResult.build(ItooResult.FAIL, "名称重复!");
            }
        }
        boolean numberRows = examinationService.updataExamInfomation(examineeModel);
        return ItooResult.build(ItooResult.SUCCESS, "编辑成功！", numberRows);

    }

    /**
     * 考试名称验重(创建)
     *
     * @param examName 考试名称
     * @return examinationService.selExamName(examName)
     * @author maying
     * @since 2018年11月30日19:51:39
     */
    @ApiOperation(value = "考试名称验重")
    @GetMapping(value = "/selExamName/{examName}")
    public ItooResult saveExamation(@PathVariable String examName) {
        if (StringUtils.isEmpty(examName)) {
            return ItooResult.build(ItooResult.FAIL, "考试名称");
        }
        try {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功！", examinationService.selExamName(examName));
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "查询失败！");
        }
    }

    /**
     * 考试信息管理--考试插入
     *
     * @param examinationModel
     * @return 插入的考试id
     * @author maying
     * @since 2018-12-13 20:50:54
     */
    @ApiOperation(value = "考试信息管理--考试插入")
    @PostMapping(value = "/saveExam")
    public ItooResult saveExam(@RequestBody ExaminationModel examinationModel) {
        String examId = examinationService.saveExam(examinationModel);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功!", examId);
    }

    /**
     * 获取系统时间
     *
     * @return ItooResult
     * @author 于云秀
     * @since 2018年12月17日
     */
    @ApiOperation(value = "获取系统时间")
    @GetMapping(value = "/selTime")
    public ItooResult selTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        return ItooResult.build(ItooResult.SUCCESS, "查询成功!", time);
    }

    /**
     * 根据考试id集合查询考试信息
     *
     * @param examIdList 考试id集合
     * @return List<Examination>
     * @author 马莹
     * @since 2018-12-26 19:56:37
     */
    @ApiOperation(value = "根据考试id集合查询考试信息")
    @GetMapping(value = "/selectExam/{examIdList}")
    public ItooResult selectExam(@PathVariable List<String> examIdList) {
        List<ExaminationModel> examinationModels = examinationService.selectExam(examIdList);
        if (examinationModels != null) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功!", examinationModels);
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询失败!");
        }
    }

    /**
     * 查询已发布的考试
     *
     * @param operator 登录id
     * @return List<Examination>
     * @author 魏恩君
     * @since 2019-1-26 10:44:05
     */
    @ApiOperation(value = "查询已发布的考试")
    @GetMapping(value = "/queryReleasedPractice/{operator}")
    public ItooResult queryReleasedPractice(@PathVariable String operator) {
        if (StringUtils.isEmpty(operator)) {
            return ItooResult.build(ItooResult.FAIL, "operator不能为空");
        }
        List<ExaminationModel> examinationModels = examinationService.queryReleasedPractice(operator);
        if (examinationModels != null) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功!", examinationModels);
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询失败!");
        }
    }

    /**
     * 更新考试状态
     *
     * @param examID 考试id
     * @return
     * @Param state 状态
     * @author 魏恩君
     * @since 2.0.0 2019/1/26
     */
    @ApiOperation(value = "更新考试状态")
    @GetMapping(value = "/updateExamState/{examID}/{state}/{classId}")
    public ItooResult updateExamState(@PathVariable String examID, @PathVariable String state, @PathVariable String classId) {
        if (StringUtils.isEmpty(examID)) {
            return ItooResult.build(ItooResult.FAIL, "考试id为空");
        }
        if (StringUtils.isEmpty(state)) {
            return ItooResult.build(ItooResult.FAIL, "状态值不能为空");
        }
        if (StringUtils.isEmpty(classId)) {
            return ItooResult.build(ItooResult.FAIL, "班级id不能为空");
        }
        int i = examinationService.updateExamState(examID, state, classId);
        if (i > 0) {
            return ItooResult.build(ItooResult.SUCCESS, "更新成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "更新失败");
        }
    }


    /**
     * @Description: 根据考试Id批量导出学生试卷成PDF文件接口
     * @Param: examinationId:考试id
     * @Param: templateId:模板Id
     * @return: 批量导出是否成功的状态
     * @Author: 杨光彩
     * @Date: 5/10/2019
     */
    @ApiOperation(value = "根据考试Id批量导出学生试卷成PDF文件接口")
    @GetMapping(value = {"/exportExamPaperToPdfByExamId/{classId}/{examinationId}/{examinationName}/{templateId}"})
    public ItooResult exportExamPaperToPdfByExamId(HttpServletResponse response, @PathVariable String classId, @PathVariable String examinationId, @PathVariable String examinationName, @PathVariable String templateId) {
        if (StringUtils.isEmpty(examinationId)) {
            return ItooResult.build(ItooResult.FAIL, "考试id不能为空");
        } else if (StringUtils.isEmpty(templateId)) {
            return ItooResult.build(ItooResult.FAIL, "模板Id不能为空");
        }
        byte[] flag = new byte[0];
        try {
            flag = examinationService.exportExamPaperToPdfByExamId(classId, examinationId, examinationName, templateId);
            response.setHeader("Content-Length", String.valueOf(flag.length));
            response.setContentType("multipart/form-data;charset=UTF-8");
            String fileName = null;
            fileName = new String(examinationName.getBytes("gb2312"), "iso8859-1");
            response.setHeader("Content-disposition", "attachment;filename=\"" + fileName+"\".zip" );
            response.getOutputStream().write(flag);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        if (flag != null) {
            return ItooResult.build(ItooResult.SUCCESS, "该考场学生试卷打印完毕！");
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "该考场学生试卷打印出错，请联系管理员！");
        }
    }
}
